package com.demo.support.Interceptor;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.demo.entity.MeUser;
import com.demo.web.base.BaseModel;
import com.jweb.annotaion.JValue;
import com.jweb.core.handle.ActionException;
import com.jweb.core.inter.HandleInterceptor;
import com.jweb.util.HttpServletRequestUtil;
import com.jweb.util.WebUtil;

/** 请求拦截 */
public class JHandleInter extends HandleInterceptor {
	private Logger log = LoggerFactory.getLogger(getClass());
	// 保存整个容器里面的用户session_id Map<userId,sessionId>
	public static Map<Long, String> userSessionIdMap = new HashMap<Long, String>();
	public static Map<String, Integer> checkForm = new HashMap<String, Integer>();
	public static ThreadLocal<MeUser> curUser = new ThreadLocal<MeUser>();
	public String curURL;
	@JValue("resources.url")
	public String resourceUrl;

	public static final String USER = "USER";
	public static final String MSG = "MSG";

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response) throws ActionException, Exception {
		MeUser t = (MeUser) WebUtil.getSessionAttribute(request, USER);
		curUser.set(t);// 当前用户
		request.setAttribute("USER", t);// 当前用户
		// ================================================
		curURL = request.getRequestURI();
		log.info("==来了一个新请求[" + request.getMethod() + "]:" + curURL);
		log.info("==参数：" + JSON.toJSONString(HttpServletRequestUtil.getParameterMap(request)));
		// 获取忽略菜单
		// Map<String, String> ignoreUrlMap = server.ignoreUrlMap;
		// if(curURL.endsWith("Action")) curURL=curURL.substring(0,
		// curURL.length()-6);
		// if (ignoreUrlMap.get(curURL.replace("Action", "")) == null &&
		// curURL.indexOf("/ajax") < 0) {// 如果不是忽略菜单
		// Test t = (Test) WebUtil.getSessionAttribute(request, "loginUser");
		// if (t == null) {
		// // 判断登录是否失效,必须先登录
		// response.sendRedirect(request.getContextPath() +
		// "/test/testLogin.html");
		// return false;
		// } else {
		// // 验证用户登录的唯一性
		// String userSessionId = userSessionIdMap.get(t.getId());
		// if (!FormatUtil.isEmpty(userSessionId) &&
		// !WebUtil.getSessionId(request).equals(userSessionId)) {
		// log.info("【账户在其它地方登录了】user:" + JSON.toJSONString(t));
		// WebUtil.setSessionAttribute(request, "loginUser", null);
		// request.setAttribute("BASE", new BaseModel(BaseModel.ERROR,
		// "您的账号已在其它地方登录"));
		// request.getRequestDispatcher("/").forward(request, response); // 转发
		// return false;
		// }
		// }
		// // ............
		// // 执行一系列验证
		//
		// } else {
		// }
		return true;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex) throws ActionException {

		request.setAttribute("OnLine", userSessionIdMap.size());// 在线人数
		request.setAttribute("JSID", WebUtil.getSessionId(request));// 当前SessionID
		if (request.getAttribute(MSG) == null)
			request.setAttribute(MSG, new BaseModel());
		request.setAttribute("TIME", new Date().getTime());
		request.setAttribute("resourceUrl", resourceUrl);

	}

}
